package pailaidai.hc.com.mysvgdemo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import java.util.Random;

import pailaidai.hc.com.mysvgdemo.R;

/**
 * Created by Administrator on 2018/4/8.
 */

public class CodeView extends View {
    private static final String TAG = "CodeView";
    private int count;//验证码的数字个数
    private int lineCount;//干扰线的条数
    private int fontSize;//字体大小
    private int color;//字体颜色
    private String code;//验证码
    private Random rnd;
    private Paint paint;
    private static final int DEFAULT_COUNT = 4;
    private static final int DEFAULT_LINE_COUNT = 50;
    private static final int DEFAULT_FONT_SIZE = 12;//SP
    private static final int DEFAULT_COLOR = Color.BLACK;


    public CodeView(Context context) {
        super(context);
    }

    public CodeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CodeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CodeView);
        count = a.getInt(R.styleable.CodeView_count, DEFAULT_COUNT);
        lineCount = a.getInt(R.styleable.CodeView_line_count, DEFAULT_LINE_COUNT);
        fontSize = a.getDimensionPixelSize(R.styleable.CodeView_font_size, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, DEFAULT_FONT_SIZE, getResources().getDisplayMetrics()));
        color = a.getColor(R.styleable.CodeView_code_color, DEFAULT_COLOR);
        a.recycle();
        rnd = new Random();
        paint = new Paint();
        initPaint();
        code = getCode();

    }

    private void initPaint() {
        paint.reset();
        paint.setAntiAlias(true);
        paint.setColor(color);
        paint.setTextSize(fontSize);
    }


    public String getCode() {
        String str = "";
        for (int i = 0; i < count; i++) {
            str += rnd.nextInt(10);
        }
        return str;
    }

    /*计算组件宽度*/
    private int measureWidth(int widthMeasureSpec, Rect textRect) {
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        int width = 0;
        if (mode == MeasureSpec.EXACTLY) {
            width = size;
        } else if (mode == MeasureSpec.AT_MOST) {
            width = getPaddingLeft() + textRect.width() + getPaddingRight();
        }
        return width;
    }

    /*计算组件的高度*/
    private int measureHeight(int heightMeasureSpec, Rect textRect) {
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        int size = MeasureSpec.getSize(heightMeasureSpec);
        int height = 0;
        if (mode == MeasureSpec.EXACTLY) {
            height = size;
        } else if (mode == MeasureSpec.AT_MOST) {
            height = getPaddingTop() + textRect.height() + getPaddingBottom();
        }
        return height;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Rect textRect = getTextRect();
        int width=measureWidth(widthMeasureSpec,textRect);
        int height=measureHeight(heightMeasureSpec,textRect);
        setMeasuredDimension(width,height);
    }

    public Rect getTextRect() {
        //根据Paint设置的绘制参数计算文字所占的宽度
        Rect rect=new Rect();
        //文字多占的区域大小保存在rect中
        paint.getTextBounds(getCode(),0, getCode().length(),rect);
        return rect;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.i(TAG,"CODE:"+code);
        int width=getMeasuredWidth();
        int height=getMeasuredHeight();
        Rect rect=new Rect(0,0,width,height);
        //绘制外围矩形框
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(1);
        Rect rect1=new Rect(rect);
        rect1.inset(2,2);//缩小一点
        canvas.drawRect(rect1,paint);
        paint.setStyle(Paint.Style.FILL);
        //绘制随机干扰线
        paint.setColor(Color.GRAY);
        for (int i=0;i<lineCount;i++){
            int x1=rnd.nextInt(width);
            int y1=rnd.nextInt(height);
            int x2=rnd.nextInt(width);
            int y2=rnd.nextInt(height);
            canvas.drawLine(x1,y1,x2,y2,paint);
        }
      paint.setColor(color);
        //绘制文字
        Rect textRect=getTextRect();
        Paint.FontMetrics fontMetrics=paint.getFontMetrics();
        int x=(width-textRect.width())/2;
        int y= (int) (height/2+(fontMetrics.descent-fontMetrics.ascent)/2-fontMetrics.descent);
        canvas.drawText(code,x,y,paint);
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
        code=getCode();
        requestLayout();//从新调整布局大小
    }

    public int getLineCount() {
        return lineCount;
    }

    public void setLineCount(int lineCount) {
        this.lineCount = lineCount;
        invalidate();//重绘
    }

    public int getFontSize() {
        return fontSize;
    }

    public void setFontSize(int fontSize) {
        this.fontSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,fontSize,getResources().getDisplayMetrics());
        initPaint();
        requestLayout();//重新调整布局大小
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
        initPaint();
        invalidate();//重绘
    }
    /*刷新验证码*/
    public void refresh(){
        code=getCode();
        invalidate();
    }
     public String theCode(){
         return code;
     }



}
